package com.zpark.dao.impl;

import com.zpark.dao.AdminDao;
import com.zpark.entity.Admin;
import com.zpark.utils.DaoUtil;

import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminDaoImpl implements AdminDao {
    @Override
    public Admin selectByUsernameAndPassword(String username, String password) {
        Admin admin = null;
        String sql = "select * from admin where username = ? and password = ?";
        try {
            ResultSet resultSet = DaoUtil.executeQuery(sql, username, password);
            while (resultSet.next()) {
                admin = new Admin();
                admin.setId(resultSet.getInt("id"));
                admin.setUsername(resultSet.getString("username"));
                admin.setPassword(resultSet.getString("password"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return admin;
    }

    @Override
    public void updatePasswordById(Integer id, String newPassword) {
        String sql = "update admin set password = ? where id = ?";
        try {
            DaoUtil.executeUpdate(sql, newPassword, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int selectByIdAndPassword(Integer id, String oldPassword) {
        int i = 0;
        String sql = "select * from admin where id = ? and password = ?";
        try {
            ResultSet resultSet = DaoUtil.executeQuery(sql, id, oldPassword);
            while (resultSet.next()) {
                i = 1;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return i ;
    }
}
/**
 *
 */
